package org.qqcui.designpattern.principles.dip;

/**
 * 依赖倒置原则示例二，正确的依赖方式，应该依赖于接口或抽象类，因为抽象的东西要稳定的多
 *
 * @author cuiqingqiang
 * @version 1.0
 * @date 2020-10-13 22:21
 **/
public class DependencyInversion2 {
	public static void main(String[] args) {
		Man man = new Man();
		// 客户端无需改变
		man.receive(new WeiXin());
		man.receive(new Message());
	}
}

/**
 * 接收消息的接口定义
 */
interface IReceiver {
	String receive();
}


/**
 * 接收微信消息的具体实现
 */
class WeiXin implements IReceiver {

	@Override
	public String receive() {
		return "接收微信信息：Hello,WeiXin";
	}
}

/**
 * 接收短信消息的具体实现
 */
class Message implements IReceiver {

	@Override
	public String receive() {
		return "接收短信信息：Hello,Motor";
	}
}

class Man {
	/**
	 * 接收消息依赖于接口，在实际接收消息时传递对应的实现类，这样的业务逻辑更稳定
	 * @param receiver 接收消息对象
	 */
	public void receive(IReceiver receiver) {
		System.out.println(receiver.receive());
	}
}